<?php


namespace app\bonus\logic;


use app\bonus\model\Region;
use app\bonus\model\WapShop;

class WapshopLogic
{
    /**
     * 验证数据
     * @param $data
     * @return array|bool
     */
    public function verifyFun($data,$type = 'add'){
        //返回值
        $rdata = array("code"=>0,"meg"=>"");
        //验证代理商名称
        if(empty($data['name'])){
            $rdata['code'] = 9999;
            $rdata['meg'] = "代理商名称不能为空";
            return $rdata;
        }

        //查询代理商名称是否存在
        $wdata['name'] = $data['name'];
        if(array_key_exists("id",$data)) $wdata['id'] = array("NEQ",$data['id']);
        $num = $this->getNameNum($wdata);
        $wdata = null;
        if($num > 0){
            $rdata['code'] = 9998;
            $rdata['meg'] = "代理商名称已经存在";
            return $rdata;
        }

        if($type == "add"){
            //验证登录账号
            if(empty($data['login_name'])){
                $rdata['code'] = 9999;
                $rdata['meg'] = "登录帐号不能为空";
                return $rdata;
            }

            //查询代理商名称是否存在
            $wdata['login_name'] = $data['login_name'];
            if(array_key_exists("id",$data)) $wdata['id'] = array("NEQ",$data['id']);
            $num = $this->getNameNum($wdata);
            if($num > 0){
                $rdata['code'] = 9998;
                $rdata['meg'] = "登录帐号已经存在";
                return $rdata;
            }

            //验证密码
            if(empty($data['password']) && $type == "add"){
                $rdata['code'] = 9997;
                $rdata['meg'] = "密码不能为空";
                return $rdata;
            }

            if ($data['password'] !== $data['password_confirm']  && $type == "add") {
                $this->error = '确认密码不正确';
                return false;
            }
        }
        return $rdata;
    }

    /**
     * 验证数据
     * @param $data
     * @return array|bool
     */
    public function verifyPwdFun($data){
        //返回值
        $rdata = array("code"=>0,"meg"=>"");
        //验证代理商名称

        //验证登录账号
        if(empty($data['login_name'])){
            $rdata['code'] = 9999;
            $rdata['meg'] = "登录帐号不能为空";
            return $rdata;
        }

        //查询代理商名称是否存在
        $wdata['login_name'] = $data['login_name'];
        if(array_key_exists("id",$data)) $wdata['id'] = array("NEQ",$data['id']);
        $num = $this->getNameNum($wdata);
        if($num > 0){
            $rdata['code'] = 9998;
            $rdata['meg'] = "登录帐号已经存在";
            return $rdata;
        }

        //验证密码
        if(empty($data['password'])){
            $rdata['code'] = 9997;
            $rdata['meg'] = "密码不能为空";
            return $rdata;
        }

        if ($data['password'] !== $data['password_confirm']) {
            $this->error = '确认密码不正确';
            return false;
        }
        return $rdata;
    }

    //获取数据
    public function getNameNum($wdata){
        return WapShop::where($wdata)->count();
    }

    /**
     * 添加
     * @param $data
     * @return int|string
     */
    public function addData($data){
        //添加数据
        $adata = DealParamLogic::dealShopPram($data);
        $adata['add_time'] = date("Y-m-d H:i:s");
        $res = WapShop::insertGetId($adata);
        return $res;
    }

    /**
     * 添加
     * @param $data
     * @return int|string
     */
    public function editData($data){
        //添加数据
        $adata = DealParamLogic::dealShopPram($data,"edit");
        $adata['add_time'] = date("Y-m-d H:i:s");
        $res = WapShop::where(['id'=>$data['id']])->Update($adata);
        return $res;
    }

    /**
     * 编辑密码
     * @param $data
     * @return int|string
     */
    public function editPwdData($data){
        //添加数据
        $adata = [];
        $adata['login_name'] = $data['login_name'];
        $adata['login_pass'] = md5($data['password']);
        $adata['add_time'] = date("Y-m-d H:i:s");
        $res = WapShop::where(['id'=>$data['id']])->Update($adata);
        return $res;
    }

    /**
     * 获取数据
     * @param $id
     * @return array|bool|false|\PDOStatement|string|\think\Model|null
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getData($id){
        $rdata = WapShop::where(['id'=>$id])->field(true)->find();
        if($rdata) $rdata = $rdata->toArray();
        return $rdata;
    }

    public function getListData(){
        $rdata = WapShop::field("id,name")->select();
        if($rdata) $rdata = $rdata->toArray();
        return $rdata;
    }

    /**
     * 获取区域
     * @param int $pid
     * @return array|bool|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRegion($pid = 0){
        $wdata['pid'] = $pid;
        $rdata = Region::where($wdata)->field("id,name,lng,lat")->select();
        if($rdata) $rdata = $rdata->toArray();
        return $rdata;

    }
}